package com.wolfword.crawler.client;

import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;

import com.wolfword.crawler.client.config.Config;
import com.wolfword.crawler.client.listener.ApplicationReady;

 
 
@SpringBootApplication
@MapperScan("com.wolfword.crawler.client.dao")
@ComponentScan
public class Application extends SpringBootServletInitializer{
	
	private static final Logger logger = LoggerFactory.getLogger(Application.class);
	
	
	@Override
	protected SpringApplicationBuilder configure(
			SpringApplicationBuilder
			builder) {
		builder.sources(this.getClass());
		return super.configure(builder);
	}
	/**
	 * 客户端启动
	 * 如果打包成jar文件运行，在执行时，可设置如下参数
	 * （1）server:服务端地址
	 * （2）crawlThread:默认启动的爬虫线程数
	 * 默认从配置文件中读取
	 * @param args
	 * @author ServerZhang
	 * @date 2018年1月22日
	 */
	public static void main(String[] args) {
		if (args.length > 0) {
			for(String para : args){
				String trim = para.trim();
				if (trim.startsWith("server")) {
					String replace = trim.replace("server:", "");
					if (!replace.startsWith("http[s]://")) {
						replace = "http://"+replace;
					}
					if (!replace.endsWith("[/\\]")) {
						replace+="/";
					}
					Config.serverHost=replace;
					logger.info("请求服务器地址为："+replace);
				}else if (trim.toLowerCase().startsWith("crawlthread")) {
					String replace = trim.toLowerCase().replace("crawlthread:", "");
					Config.crawlerThreadCount = Integer.parseInt(replace);
					logger.info("爬虫开启线程数为："+replace);
				}
			}
		}
		
		SpringApplication application = new SpringApplication(Application.class);
		application.addListeners(new ApplicationReady());
		application.run(args); 
	}
}
